#!/usr/bin/env python3
"""
添加邮件任务附件字段
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from sqlalchemy import text
from database import engine, SessionLocal

def add_attachments_field():
    """添加attachments字段到email_tasks表"""
    db = SessionLocal()
    try:
        # 检查字段是否已存在
        result = db.execute(text("""
            SELECT COLUMN_NAME 
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_NAME = 'email_tasks' 
            AND COLUMN_NAME = 'attachments'
            AND TABLE_SCHEMA = DATABASE()
        """))
        
        if result.fetchone():
            print("attachments字段已存在，跳过添加")
            return
        
        # 添加attachments字段
        db.execute(text("""
            ALTER TABLE email_tasks 
            ADD COLUMN attachments JSON COMMENT '附件信息'
        """))
        
        db.commit()
        print("成功添加attachments字段到email_tasks表")
        
    except Exception as e:
        print(f"添加attachments字段失败: {e}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    add_attachments_field()
